package sorting;

public class BubbleSorting extends Sorting{

    private int[] datas;
    
    public BubbleSorting() {
        super();
    }
    
    public BubbleSorting(int[] datas) {
        super(datas);
        this.datas = datas;
    }
    
    public void sort() {
        if (datas == null || datas.length == 0) {
            System.out.println("No data to sort");
            return;
        }
        boolean unsorted = false;
        for (int i = 0; i < datas.length; i++) {
            for (int j = 0; j < datas.length-i-1; j++) {
                if (datas[j] > datas[j+1]) {
                    swap(j, j+1);
                    unsorted = true;
                }
            }
            if (!unsorted) {
                return;
            }
        }
    }
    
    private void swap(int one, int two) {
        int temp = datas[one];
        datas[one] = datas[two];
        datas[two] = temp;
    }
}